package com.kitty.game.activity.service.other;


import com.kitty.common.core.SchedulerManager;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.activity.model.product.SuperBoss;
import com.kitty.game.activity.service.time.FightActivityHandler;
import com.kitty.game.boss.config.BossSet;
import com.kitty.game.boss.model.BossFightParam;
import com.kitty.game.boss.model.BossParam;
import com.kitty.game.boss.model.SuperBossParam;
import com.kitty.game.chat.service.ChatService;
import com.kitty.game.config.NPC;
import com.kitty.game.config.TaskSet;
import com.kitty.game.enter.Position;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.fight.ai.model.RoundSkillUnit;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.game.fight.factory.model.NewBossFight;
import com.kitty.game.fight.message.RespCombatAddOpponent;
import com.kitty.game.fight.message.vo.CombatAddObject;
import com.kitty.game.fight.message.vo.FightMember;
import com.kitty.game.fight.service.FightMessageService;
import com.kitty.game.fight.util.FightAction;
import com.kitty.game.fight.util.FightMessageUtil;
import com.kitty.game.i18n.I18nId;
import com.kitty.game.i18n.I18nIdDataPool;
import com.kitty.game.map.service.MapService;
import com.kitty.game.role.model.Role;
import com.kitty.game.task.message.vo.TaskInfo;
import com.kitty.game.task.model.user.RoleTask;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.game.team.model.Team;
import com.kitty.game.utils.Const;
import com.kitty.game.utils.DateUtils;
import com.kitty.game.utils.TimeUtil;
import com.kitty.listener.event.FightEndEvent;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.message.MessagePusher;
import org.nutz.lang.util.NutMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

/**超级Boss处理类*/
@Component
public class YaBiaoBossHandler extends FightActivityHandler {

    private final Logger logger = LoggerFactory.getLogger(YaBiaoBossHandler.class);
    /**
     * 出现的地图ID列表
     */
    private static final List<Integer> MAPIDS = Arrays.asList(13000, 14000, 15000, 16000, 10200, 10300, 10400, 10500, 16100, 18000, 17000, 17100,
            17200, 17400, 17500, 17600, 17700, 22000, 19002);
    private static final List<Byte> POLARS = Arrays.asList(Const.SCHOOL_METAL, Const.SCHOOL_WOOD, Const.SCHOOL_WATER, Const.SCHOOL_FIRE, Const.SCHOOL_EARTH);
    /**各门派的攻击技能*/
    private static final Map<Byte, Integer> POLAR_SKILLS = new HashMap<>();
    /**各门派的障碍技能*/
    private static final Map<Byte, Integer> POLAR_HELP_SKILLS = new HashMap<>();

//    private static final List<String> SUPER_BOSS_LIST = Arrays.asList("炼狱冥炎","黑熊妖皇", "血炼魔猪", "魅影蝎后", "赤血鬼猿","钧天君");
//    private static final List<String> SUPER_BOSS_LIST = Arrays.asList("黑熊妖皇", "血炼魔猪", "魅影蝎后","钧天君");
    private static final List<String> SUPER_BOSS_LIST = Arrays.asList("钧天君");
    private static final String CONTENT = "此路是我开,此树是我栽,打从此路过,留下买路财！[【押送镖银】赶紧闪开/fight_qiangdao_yb][糟了！押镖遇到强盗了，赶紧逃命/逃命]";
    private static final String CONTENT2 = "此路是我开,此树是我栽,打从此路过,留下买路财！[【押送镖银】速速闪开/fight_qiangdao_tf][糟了！押镖遇到土匪了，赶紧逃命/逃命]";
    private static final String START_ACCEPT_SYSTEM = "洪水泛滥，灾民煎熬，天墉城百姓纷纷集资赈灾，托杨镖头护送赈灾镖银。镖局人手不足召侠义之士一起护送赈灾镖银。请前往#Z天墉城|::杨镖头|E=【押镖】领取押送镖银任务|$0#Z";

    /**
     * 挑战最小等级
     */
    private static final int MIN_LEVEL = 79;
    /**
     * 队员之间最大等级差
     */
    private static final int MAX_LEVEL_DIFF = 100;
    /**
     * 最少组队人数
     */
    private static final int MIN_TEAM_COUNT = 3;
    /**
     * 最大战胜次数
     */
    private static final short MAX_WIN_COUNT = 1;

    private static int qishaId=0;

    private static int jishuId=0;


    @Autowired
    private MapService mapService;
    @Autowired
    ChatService chatService;


    private Map<String, SuperBoss> superBossMap = new HashMap<>();

    @PostConstruct
    private void init() {
        /**各门派的5阶攻击技能*/
        POLAR_SKILLS.put(Const.SCHOOL_METAL, 15);
        POLAR_SKILLS.put(Const.SCHOOL_WOOD, 65);
        POLAR_SKILLS.put(Const.SCHOOL_WATER, 114);
        POLAR_SKILLS.put(Const.SCHOOL_FIRE, 165);
        POLAR_SKILLS.put(Const.SCHOOL_EARTH, 214);

        /**各门派的5阶障碍技能*/
        POLAR_HELP_SKILLS.put(Const.SCHOOL_METAL, 25);
        POLAR_HELP_SKILLS.put(Const.SCHOOL_WOOD, 75);
        POLAR_HELP_SKILLS.put(Const.SCHOOL_WATER, 125);
        POLAR_HELP_SKILLS.put(Const.SCHOOL_FIRE, 175);
        POLAR_HELP_SKILLS.put(Const.SCHOOL_EARTH, 225);


        SuperBoss superBoss = new SuperBoss("安静的灵兽", 6189, 702);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(11,12,13,14,15,31,32,33,34,35,25) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(61,62,63,64,65,81,82,83,84,85,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(110,111,112,113,114,131,132,133,134,135,125) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(161,162,163,164,165,181,182,183,184,185,175) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(210,211,212,213,214,231,232,233,234,235,225) );
        superBossMap.put(superBoss.getName(), superBoss);


        superBoss = new SuperBoss("冰晶龙鳞兽王", 6162, 702);
        superBoss.setSmallBossName("冰晶兽侍卫");
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(11,12,13,14,15,31,32,33,34,35,25) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(61,62,63,64,65,81,82,83,84,85,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(110,111,112,113,114,131,132,133,134,135,125) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(161,162,163,164,165,181,182,183,184,185,175) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(210,211,212,213,214,231,232,233,234,235,225) );
        superBossMap.put(superBoss.getName(), superBoss);


        superBoss = new SuperBoss("雪狐王", 6271, 702);
        superBoss.setSmallBossName("雪狐");
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(11,12,13,14,15,31,32,33,34,35,25) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(61,62,63,64,65,81,82,83,84,85,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(110,111,112,113,114,131,132,133,134,135,125) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(161,162,163,164,165,181,182,183,184,185,175) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(210,211,212,213,214,231,232,233,234,235,225) );
        superBossMap.put(superBoss.getName(), superBoss);


        superBoss = new SuperBoss("仙界的亡魂", 6305, 702);
        superBoss.setSmallBossName("凶魂");
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(11,12,13,14,15,31,32,33,34,35,25) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(61,62,63,64,65,81,82,83,84,85,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(110,111,112,113,114,131,132,133,134,135,125) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(161,162,163,164,165,181,182,183,184,185,175) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(210,211,212,213,214,231,232,233,234,235,225) );
        superBossMap.put(superBoss.getName(), superBoss);

        superBoss = new SuperBoss("百变灵兽", 6312, 702);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(11,12,13,14,15,31,32,33,34,35,25) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(61,62,63,64,65,81,82,83,84,85,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(110,111,112,113,114,131,132,133,134,135,125) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(161,162,163,164,165,181,182,183,184,185,175) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(210,211,212,213,214,231,232,233,234,235,225) );
        superBossMap.put(superBoss.getName(), superBoss);

        superBoss = new SuperBoss("斗战仙官", 6236, 702);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(11,12,13,14,15,31,32,33,34,35,25) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(61,62,63,64,65,81,82,83,84,85,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(110,111,112,113,114,131,132,133,134,135,125) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(161,162,163,164,165,181,182,183,184,185,175) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(210,211,212,213,214,231,232,233,234,235,225) );
        superBossMap.put(superBoss.getName(), superBoss);



    }

    /**
     * 推送开始可以接取任务的系统消息
     */
    public void startYaBiao(String cron_end) {
        //SpringUtils.getChatService().sendSystem(START_ACCEPT_SYSTEM, Const.BRODCAST_MSG_TYPE_ROLE);
        SpringUtils.getChatService().sendAdnotice(START_ACCEPT_SYSTEM+"    押镖活动于#Y"+cron_end+"#n结束");
        DataCache.REDIS_DATA.put("yabiao_status","1");
    }
    public void endYaBiao() {
        DataCache.REDIS_DATA.put("yabiao_status","0");
    }


    public boolean isInTime(){
        return true;
    }

    @Override
    protected String getNpcContent(Role role, NPC bossNpc) {
        return null;
    }

    @Override
    protected String getNpcContentNotFight(Role role, NPC bossNpc) {

        return null;
    }

    @Override
    protected void doStartFight(Role role, NPC bossNpc) {

        SuperBoss superBoss = getSuperBoss(bossNpc.getName());

        List<BossParam> bossParamList = newBossParamList(role, bossNpc);

        BossFightParam bossFightParam = new BossFightParam(bossParamList, getFightType(role));
        bossFightParam.setNpcId(bossNpc.getId());
        Fight fight = bossService.startFightToBoss(role, bossFightParam);

    }

    private void addShouting(Fight fight) {
        if (fight.getData() == null) {
            fight.setData(NutMap.NEW());
        }
        String content = I18nIdDataPool.getI18nContent(I18nId.PMT_1008);
        fight.getData().setv("fight_round_start_shouting", Arrays.asList(content));
        fight.getData().setv("fight_round_start_shouting_period", 5);
    }

    private List<BossParam> newBossParamList(Role role, NPC bossNpc) {
        List<BossParam> bossParamList = new ArrayList<>();
        int count = getBossCount(role);

        /**npc对应的加在第1个*/
        BossSet bossSet = bossService.getBossSet(bossNpc.getBossSetName());
        bossParamList.add(new BossParam(bossSet, bossNpc.getName()));

        /**加count-1个*/
        for (int i=1; i<count; i++) {
            bossSet = bossService.getBossSet(bossNpc.getBossSetName());
            bossParamList.add(new BossParam(bossSet, bossSet.getName()));
        }

        return bossParamList;
    }

    private SuperBossParam newSuperBossParam(NPC bossNpc) {
        SuperBoss superBoss = getSuperBoss(bossNpc.getName());
        BossSet bossSet = bossService.getBossSet(bossNpc.getBossSetName());
        SuperBossParam bossParam = new SuperBossParam(bossSet, bossNpc.getName());


        return bossParam;
    }

    /**获得随机相性*/
    private byte getRandomPolar() {
        int index = ThreadLocalRandom.current().nextInt(POLARS.size());
        return POLARS.get(index);
    }

    /**获得匹配相性的技能ID列表*/
    private ArrayList<Integer> getSkillIdsMatchPolar(byte polar) {
        ArrayList<Integer> skilIds = new ArrayList<>();
        /**相性的技能*/
        Integer skillId = POLAR_SKILLS.get(polar);
        skilIds.add(skillId);

        /**力破千钧*/
        skilIds.add(501);
        /**普通攻击*/
        skilIds.add(2);

        return skilIds;
    }

    private List<RoundSkillUnit> getRoundSkills(byte polar, SuperBoss superBoss) {
        List<RoundSkillUnit> list = new ArrayList<>();
        /**妖皇天怒*/
        list.add(new RoundSkillUnit(5, 5, superBoss.getSkillId()));
        Integer skillId = POLAR_HELP_SKILLS.get(polar);
        list.add(new RoundSkillUnit(6, 5, skillId));

        return list;
    }

    @Override
    protected int getFightType(Role role) {
        return Const.fightType_yabiao;
    }

    @Override
    protected NPC getBossNpc(int npcId) {
        return mapService.getNpc(npcId);
    }

    @Override
    protected void clearNpcAfterWin(Role role, NPC bossNpc) {

    }

    public TaskInfo getTaskInfo(Role role, TaskSet taskSet) {
        TaskInfo taskInfo = SpringUtils.getTaskService().getTaskInfo(role, taskSet);
        taskInfo.setTaskZhiyin(MessageFormat.format(taskSet.getTaskZhiyin(), role.getNoDeath()));
        taskInfo.setTaskJiangli(MessageFormat.format(taskSet.getTaskGift(), role.getNoDeath()));
        return taskInfo;
    }

    private void giveRewardOne(Role role, NPC bossNpc){
        NewTaskService newTaskService = SpringUtils.getTaskService();
        logger.debug("当前任务: 除掉劫镖强盗=={}=={}", role.getName(),bossNpc.getName());
        if(bossNpc.getName().equals("劫镖强盗")){
            int taskId1 = 2500;
            if(role.getTask().getDoingTask().get(taskId1) == null){
                logger.debug("未接到当前任务: 除掉劫镖强盗 - {}={}",   role.getName(),bossNpc.getName());
            }
            else{
                TaskSet taskSet = newTaskService.getTaskSet(taskId1, role);
                if(taskSet != null){
                    logger.debug("当前任务: 除掉劫镖强盗{}", taskSet.getId() +  role.getName());
                    RoleTask roleTask = role.getTask();
                    roleTask.updateToFinished( taskId1);

                    TaskSet nextTask = newTaskService.getNextTask(taskSet,role);
                    if (nextTask != null) {
                        logger.error("接取下一任务: {}{}{}", nextTask.getId() ,bossNpc.getName(), role.getName());
                        role.getTask().acceptTask(nextTask.getId());
                        TaskInfo taskInfo = getTaskInfo(role, nextTask);
                        newTaskService.pushTaskInfo(role, taskInfo);
                        MessagePusher.pushMessage(role, new RespNotifyMiscEx("任务完成，接到下一任务，点击任务自动寻路"));
                    }
                }
            }
        }
        if(bossNpc.getName().equals("劫镖土匪")){
            int taskId1 = 2501;
            if(role.getTask().getDoingTask().get(taskId1) == null){
                logger.debug("未接到当前任务: 除掉劫镖土匪 - {}={}",   role.getName(),bossNpc.getName());
            }
            else{
                TaskSet taskSet = newTaskService.getTaskSet(taskId1, role);
                if(taskSet != null){
                    logger.debug("当前任务: 除掉劫镖土匪{}", taskSet.getId() +  role.getName());
                    RoleTask roleTask = role.getTask();
                    roleTask.updateToFinished( taskId1);

                    TaskSet nextTask = newTaskService.getNextTask(taskSet,role);
                    if (nextTask != null) {
                        logger.error("接取下一任务: {}{}{}", nextTask.getId() ,bossNpc.getName(), role.getName());
                        role.getTask().acceptTask(nextTask.getId());
                        TaskInfo taskInfo = getTaskInfo(role, nextTask);
                        newTaskService.pushTaskInfo(role, taskInfo);
                        MessagePusher.pushMessage(role, new RespNotifyMiscEx("任务完成，接到下一任务，点击任务自动寻路"));
                    }
                }
            }
        }


    }

    @Override
    protected void giveReward(Role role, FightEndEvent fightEndEvent, NPC bossNpc) {
         logger.debug("战斗胜利:" + role.getName()+ " boss" + bossNpc.getName()) ;
//
        Team team = teamService.getTeam(role.getRoleId());
        if(team != null && team.getLeaderRoleId() == role.getRoleId()) {
            giveRewardOne(role, bossNpc);
        }
        if(team == null){
            giveRewardOne(role, bossNpc);
        }
    }

    @Override
    protected void clearNpcTimeOut(NPC npc) {
        timeOut(npc);
    }

    @Override
    public void doFightWin(Role role, FightEndEvent fightEndEvent) {
        super.doFightWin(role, fightEndEvent);

        NPC npc = mapService.getNpc(fightEndEvent.getNpcId());
        if(npc != null){
            clearSuperBoss(npc);
        }


    }

    @Override
    public void doFightFail(Role role, FightEndEvent fightEndEvent) {
        super.doFightFail(role, fightEndEvent);
        NPC npc = mapService.getNpc(fightEndEvent.getNpcId());
        if (npc != null){
            SuperBoss superBoss = getSuperBoss(npc.getName());
            superBoss.setFightCount(superBoss.getFightCount() - 1);
        }
        teamService.memberHandleThreadLocal(role, memberRole -> SpringUtils.getRoleService().punishFightDead(memberRole));
    }

    @Override
    public void doFightRunAway(Role role, FightEndEvent fightEndEvent) {
        super.doFightRunAway(role, fightEndEvent);
        NPC npc = mapService.getNpc(fightEndEvent.getNpcId());
        SuperBoss superBoss = getSuperBoss(npc.getName());
        superBoss.setFightCount(superBoss.getFightCount() - 1);

        teamService.memberHandleThreadLocal(role, memberRole -> SpringUtils.getRoleService().punishFightDead(memberRole));
    }

    @Override
    public void doFightMaxRound(Role role, FightEndEvent fightEndEvent) {
        super.doFightMaxRound(role, fightEndEvent);
        NPC npc = mapService.getNpc(fightEndEvent.getNpcId());
        SuperBoss superBoss = getSuperBoss(npc.getName());
        superBoss.setFightCount(superBoss.getFightCount() - 1);
    }

    @Override
    public boolean isSetInFight() {
        /**不需要设置进入战斗状态，可以同时进入战斗*/
        return false;
    }


    //飞升-引路人-安静的灵兽
    public void flushBoss(Role role){
        String name = "劫镖强盗";
        /**随机出现的地图*/
        int mapId = 6000;
//        superBoss.setMapId(mapId);
        NPC npc = new NPC();
        {
            npc.setId(SpringUtils.getBossService().getTempNpcId());
            this.qishaId = npc.getId();
            npc.setX((short) 0);
            npc.setY((short) 0);
            npc.setFangxiang((short) 7);
            npc.setMapId(mapId);
            npc.setCreateTime(System.currentTimeMillis());
            npc.setEndTime(npc.getCreateTime() + 30 * TimeUtil.ONE_MINUTE);
            npc.setType(NPC.TYPE_SUPER_BOSS);
            npc.setName(name);
            npc.setBossSetName(name);
            npc.setContent(CONTENT);
            npc.setIcon(6201);

            bossService.addTaskNpc(npc);
            startFight(role, npc);
        }
    }

    public void flushBoss_tf(Role role){
        String name = "劫镖土匪";
        /**随机出现的地图*/
        int mapId = 8000;
        NPC npc = new NPC();
        {
            npc.setId(SpringUtils.getBossService().getTempNpcId());
            npc.setX((short) 0);
            npc.setY((short) 0);
            npc.setFangxiang((short) 7);
            npc.setMapId(mapId);
            npc.setCreateTime(System.currentTimeMillis());
            npc.setEndTime(npc.getCreateTime() + 30 * TimeUtil.ONE_MINUTE);
            npc.setType(NPC.TYPE_SUPER_BOSS);
            npc.setName(name);
            npc.setBossSetName(name);
            npc.setContent(CONTENT2);
            npc.setIcon(6202);

            bossService.addTaskNpc(npc);
            startFight(role, npc);
        }
    }

    private SuperBoss getSuperBoss(String name) {
        return superBossMap.get(name);
    }


    /**已经到来*/
    public void hasCome(SuperBoss superBoss) {
        NPC npc = createSuperBoss(superBoss);

        SchedulerManager.getInstance().schedule(() -> willTimeOut(npc), 20 * TimeUtil.ONE_MINUTE);
        SchedulerManager.getInstance().schedule(() -> timeOut(npc), 30 * TimeUtil.ONE_MINUTE);
    }

    private NPC createSuperBoss(SuperBoss superBoss) {
        int mapId = superBoss.getMapId();
        String name = superBoss.getName();
        Position position = getRandomPosition(mapId);
        NPC npc = new NPC();
        npc.setId(SpringUtils.getBossService().getTempNpcId());
        npc.setX(position.getX());
        npc.setY(position.getY());
        npc.setFangxiang((short) new Random().nextInt(8));
        npc.setMapId(mapId);
        npc.setCreateTime(System.currentTimeMillis());
        npc.setEndTime(npc.getCreateTime() + 30 * TimeUtil.ONE_MINUTE);
        npc.setType(NPC.TYPE_SUPER_BOSS);
        npc.setName(name);
        npc.setBossSetName(name);
        npc.setContent(I18nIdDataPool.getI18nContent(I18nId.PMT_1003));
        npc.setIcon(superBoss.getIcon());

        bossService.addTaskNpc(npc);

//        bossService.broadcastNpcShow(null, npc);
        return npc;
    }

    /**即将过期*/
    private void willTimeOut(NPC npc) {
        /**已经消失*/
        if (mapService.getNpc(npc.getId()) == null) {return ;}
    }

    private void timeOut(NPC npc) {
        /**已经消失*/
        if (mapService.getNpc(npc.getId()) == null) {return ;}

        clearSuperBoss(npc);
    }

    private void clearSuperBoss(NPC npc) {
        if (npc == null) {
            return;
        }



        bossService.delTaskNpc(npc);
    }

    /**
     * 所有怪物死亡或者每回合检查
     */
    public void checkFight(Fight fight) {
        if (fight.getData() != null && fight.getData().get("fight_super_boss_param") != null) {
            if (!fight.isSideOver(fight.getListB()) && fight.getRound() < fight.getData().getInt("fight_main_show_after_round")) {
                return ;
            }
            BossParam bossParam = fight.getData().getAs("fight_super_boss_param", SuperBossParam.class);
            fight.getData().remove("fight_super_boss_param");

            double lifeRate = 1;
            if (fight.getRound() >= fight.getData().getInt("fight_main_show_after_round")) {
                lifeRate = 1.2;
            } else if (fight.isSideOver(fight.getListB())) {
                lifeRate = 0.8;
            }

            NewBossFight bossFight = (NewBossFight)fight;
            if (fight.getListB() != null) {
                List<FightObject> list = new ArrayList<>(fight.getListB());
                for (FightObject fightObject : list) {
                    FightMessageUtil.sendFightPackage(fight, SpringUtils.getFightMessageService().disappear(fightObject.getId()));
                    fight.removeFightObject(fightObject);
                }
            }

            RespCombatAddOpponent respCombatAddOpponent = new RespCombatAddOpponent();
            respCombatAddOpponent.setList(new ArrayList<>());
            CombatAddObject combatAddObject = new CombatAddObject();
            respCombatAddOpponent.getList().add(combatAddObject);

            FightObject fightObject = bossFight.addFightObject(bossParam, (byte) 0);
            fightObject.setMaxLife((int) (fightObject.getCurrLife()*lifeRate));
            fightObject.setCurrLife(fightObject.getMaxLife());
            FightMember fightMember = null;
            for (FightMember member : bossFight.getFightMemberB()) {
                if (fightObject.getId() == member.getId()) {
                    fightMember = member;
                    break;
                }
            }

            combatAddObject.setId(fightMember.getId());
            combatAddObject.setLeader(fightMember.getLeader());
            combatAddObject.setWeapon_icon(fightMember.getWeapon_icon());
            combatAddObject.setPosition(fightMember.getPosition());
            combatAddObject.setColor(fightMember.getColor());
            combatAddObject.setVip_type(fightMember.getVipStatus());
            combatAddObject.setList(fightMember.getList());
            combatAddObject.setIcon(fightMember.getIcon());
            combatAddObject.setSuit_icon(fightMember.getSuit_icon());
            combatAddObject.setSuit_light_effect(fightMember.getSuit_light_effect());
            combatAddObject.setSpecial_icon(fightMember.getSpecial_icon());

            FightMessageUtil.sendFightPackage(fight, respCombatAddOpponent, true);

            addShouting(fight);
        }

        if (fight.getData() != null && fight.getData().get("fight_small_escape_after_round") != null
                && fight.getRound() >= fight.getData().getInt("fight_small_escape_after_round")) {
            fight.getData().remove("fight_small_escape_after_round");

            if (fight.getListB() != null) {
                FightMessageService fightMessageService = SpringUtils.getFightMessageService();
                List<FightObject> list = new ArrayList<>(fight.getListB());
                for (FightObject fightObject : list) {
                    if (fightObject.isMonster() && !fightObject.isSuperBoss()) {
                        fightObject.setType(FightAction.FLEE);
                        fightObject.setSkillId(0);
                        FightMessageUtil.sendFightPackage(fight, fightMessageService.doActionStart(fight.getRound(), fightObject));
                        FightMessageUtil.sendFightPackage(fight, fightMessageService.disappear(fightObject.getId()));
                        logger.warn("逃跑id:{}, round:{}", fightObject.getId(), fight.getRound());
                        FightMessageUtil.sendFightPackage(fight, fightMessageService.doActionEnd(fightObject.getId()));
                        fight.removeFightObject(fightObject);
                    }
                }
            }
        }
    }
}
